package com.yodawnla.fishing.scene;

import java.util.ArrayList;

import org.anddev.andengine.entity.sprite.Sprite;

import android.util.SparseArray;
import android.view.KeyEvent;

import com.yodawnla.fishing.FishRec;
import com.yodawnla.fishing.Player;
import com.yodawnla.fishing.R;
import com.yodawnla.fishing.system.Fonts;
import com.yodawnla.fishing.system.Tex;
import com.yodawnla.fishing.system.Values;
import com.yodawnla.fishing.system.YS;
import com.yodawnla.lib.YoScene;
import com.yodawnla.lib.util.widget.YoButton;
import com.yodawnla.lib.util.widget.YoText;

public class HandbookScene extends YoScene
{
	Player mPlayer = Player.getInstance();
	YoText mFishName;
	ArrayList<YoButton> mFishBtnList;
	ArrayList<YoText> mBtnTextList;
	ArrayList<Sprite> mFishSpriteList;
	ArrayList<Sprite> mFishSpriteList2;
	SparseArray<FishRec> mFishRecList;
	int mPage;
	final int mMaxPage = 2;
	final int ITEM_IN_ONE_PAGE = 6; // One page that contain max button number.

	YoText mFishNameText;
	YoText mFishSpotText;
	YoText mFishBaitText;
	YoText mFishLengthText;
	YoText mHitTimeText;
	YoText mPageText;

	@Override
	public void loadScene()
	{
		Sprite bg = new Sprite(0, 0, 480, 800, getTexture(Tex.HandbookBg));
		this.attachChild(bg);

		Sprite upperBg = new Sprite(0, 0, 480, 326, getTexture(Tex.HandbookUpperBg));
		this.attachChild(upperBg);

		YoText maxRecordText = new YoText(223, 182, Fonts.White30, getRString(R.string.MaxRecord));
		maxRecordText.setColor(0, 0, 0);
		this.attachChild(maxRecordText);
		YoText hitTimeText = new YoText(222, 235, Fonts.White30, getRString(R.string.HitTime));
		hitTimeText.setColor(0, 0, 0);
		this.attachChild(hitTimeText);
		YoText usedBait = new YoText(47, 182, Fonts.White30, getRString(R.string.UsedBait));
		usedBait.setColor(0, 0, 0);
		this.attachChild(usedBait);

		mFishBtnList = new ArrayList<YoButton>();
		mBtnTextList = new ArrayList<YoText>();
		mFishRecList = mPlayer.getFishRecs();

		mFishSpriteList = new ArrayList<Sprite>();
		mFishSpriteList2 = new ArrayList<Sprite>();

		mFishNameText = new YoText(43, 23, Fonts.White20, 10);
		this.attachChild(mFishNameText);
		mFishNameText.setColor(0, 0, 0);
		mFishSpotText = new YoText(259, 97, Fonts.White20, 40);
		mFishSpotText.setColor(0, 0, 0);
		this.attachChild(mFishSpotText);
		mFishBaitText = new YoText(76, 221, Fonts.White20, 40);
		mFishBaitText.setColor(0, 0, 0);
		this.attachChild(mFishBaitText);
		mFishLengthText = new YoText(380, 209, Fonts.White20, 8);
		mFishLengthText.setColor(0, 0, 0);
		this.attachChild(mFishLengthText);
		mFishLengthText.setPosition(380, maxRecordText.getY()+(maxRecordText.getHeight()-mFishLengthText.getHeight() )/2);
		
		mHitTimeText = new YoText(380, 256, Fonts.White20, 8);
		mHitTimeText.setColor(0, 0, 0);
		this.attachChild(mHitTimeText);
		mHitTimeText.setPosition(380, hitTimeText.getY()+(hitTimeText.getHeight()-mHitTimeText.getHeight())/2);
		
		mPageText = new YoText(220, 684, Fonts.White20, 2);
		this.attachChild(mPageText);
		mPageText.setText("" + mPage);

		YoButton fishBtn;
		YoText btnText;

		for (int i = 0; i < 3; i++)
		{
			for (int j = 0; j < 2; j++)
			{
				final int index = i * 2 + j;
				fishBtn = new YoButton(this, 13 + 234 * j, 338 + 110 * i, getTexture(Tex.HandbookBtn))
				{
					@Override
					public void onClickedEvent()
					{
						showFishRec(index);
					}

				};
				this.attachChild(fishBtn);
				mFishBtnList.add(fishBtn);

				btnText = new YoText(5, 5, Fonts.White20, 5);
				btnText.setText("?????");
				fishBtn.attachChild(btnText);
				mBtnTextList.add(btnText);
			}
		}

		for (int i = 0; i < Values.Fish_Quantity; i++)
		{
			Sprite fishSprite = new Sprite(15, 65, getTexture(Tex.Fish + i));
			this.attachChild(fishSprite);
			mFishSpriteList2.add(fishSprite);
			fishSprite.setVisible(false);
			fishSprite.setScaleCenter(0, 0);
		}

		for (int i = 0; i < Values.Fish_Quantity; i++)
		{
			Sprite fishSprite = new Sprite(0, 0, getTexture(Tex.Fish + i));
			this.attachChild(fishSprite);
			mFishSpriteList.add(fishSprite);
			fishSprite.setVisible(false);
		}

		YoButton prevBtn = new YoButton(this, 23, 664, getTexture(Tex.Arrow).deepCopy())
		{
			@Override
			public void onClickedEvent()
			{
				if (mPage == 0)
					mPage = mMaxPage - 1;
				else
					mPage--;

				update();
			}
		};
		prevBtn.setFlippedHorizontal(true);
		this.attachChild(prevBtn);

		YoButton nextBtn = new YoButton(this, 377, 664, getTexture(Tex.Arrow).deepCopy())
		{
			@Override
			public void onClickedEvent()
			{
				if (mPage == mMaxPage - 1)
					mPage = 0;
				else
					mPage++;

				update();
			}
		};
		this.attachChild(nextBtn);

		YoButton closeBtn = new YoButton(this, 410, 10, getTexture(Tex.Close))
		{
			@Override
			public void onClickedEvent()
			{
				toScene(YS.TitleScene);
			}
		};
		this.attachChild(closeBtn);

	}

	public void showFishRec(int index)
	{
		int FishRecListIndex = index + mPage * ITEM_IN_ONE_PAGE;
		FishRec fishRec = mFishRecList.get(FishRecListIndex);
		if (fishRec.getHitCount() > 0)
		{

			ArrayList<Integer> fishBaitList = fishRec.getUsedBaitList();

			mFishNameText.setText("" + fishRec.getFishName());
			mFishLengthText.setText("" + fishRec.getFishMaxLength() + getRString(R.string.cm));
			String fishBaitStr = "";
			for (int i = 0; i < fishBaitList.size(); i++)
			{
				int type = fishBaitList.get(i);
				if (type >= 100)
					fishBaitStr += getRString(R.string.bait1 + type - 100) + "\n";
				else
				{
					fishBaitStr += getRString(R.string.fish1 + type) + "\n";
				}

			}
			mFishBaitText.setText(fishBaitStr);
			mHitTimeText.setText("" + fishRec.getHitCount() + getRString(R.string.times));

			for (int i = 0; i < mFishSpriteList2.size(); i++)
			{
				mFishSpriteList2.get(i).setVisible(false);
			}

			mFishSpriteList2.get(fishRec.getFishId()).setScale(mFishSpriteList.get(fishRec.getFishId()).getScaleX());
			mFishSpriteList2.get(fishRec.getFishId()).setVisible(true);

		}
		else
		{
			mFishNameText.setText("?????");
			mFishLengthText.setText("?????");
			mFishBaitText.setText("?????");
			mHitTimeText.setText("?????");
			mFishSpotText.setText("?????");

			for (int i = 0; i < mFishSpriteList2.size(); i++)
			{
				mFishSpriteList2.get(i).setVisible(false);
			}
		}
	}

	@Override
	public void onEnterScene()
	{
		update();
	}

	public void update()
	{

		mPageText.setText("" + (mPage + 1));
		int size = mPage * ITEM_IN_ONE_PAGE;
		int CurrentPageNumber;
		if (mPage < mMaxPage - 1)
			CurrentPageNumber = ITEM_IN_ONE_PAGE;
		else
			CurrentPageNumber = mFishRecList.size() - size;

		for (int i = 0; i < mFishSpriteList.size(); i++)
		{
			mFishSpriteList.get(i).setVisible(false);
		}

		for (int i = 0; i < mFishBtnList.size(); i++)
		{
			if (i < CurrentPageNumber)
			{
				mFishBtnList.get(i).setVisible(true);
				if (mFishRecList.get(i + size).getHitCount() > 0)
				{
					mBtnTextList.get(i).setVisible(false);
					YoButton fishbtn = mFishBtnList.get(i);
					Sprite fishsprite = mFishSpriteList.get(i + size);
					fishsprite.setVisible(true);
					
					if(fishsprite.getWidth()>fishbtn.getWidth())
					{
						fishsprite.setScale(fishbtn.getWidth()/fishsprite.getWidth());
					}
					else if(fishsprite.getHeight()>fishbtn.getHeight())
					{
						fishsprite.setScale(fishbtn.getHeight()/fishsprite.getHeight());
					}
					fishsprite.setPosition(fishbtn.getX() + ((fishbtn.getWidth() - fishsprite.getWidth()) / 2),
							fishbtn.getY() + ((fishbtn.getHeight() - fishsprite.getHeight()) / 2));
				}
				else
				{
					mBtnTextList.get(i).setVisible(true);
				}

			}
			else
			{
				mFishBtnList.get(i).setVisible(false);
			}

		}
	}

	@Override
	public boolean onKeyDown(final int pKeyCode, final KeyEvent pEvent)
	{
		switch (pKeyCode)
		{
		case KeyEvent.KEYCODE_BACK:
			if (pEvent.getAction() == KeyEvent.ACTION_DOWN)
			{
				toScene(YS.TitleScene);
				return true;
			}
			break;
		case KeyEvent.KEYCODE_MENU:
			if (pEvent.getAction() == KeyEvent.ACTION_DOWN)
			{

				return true;
			}
			break;
		}
		return false;
	}
}
